<script setup>

import {Delete, DocumentAdd, Edit} from "@element-plus/icons-vue";
import {ref} from "vue";
import requestUtil from "@/utils/request";
import Dialog from '@/views/sys/menu/components/dialog.vue'
import {ElMessage} from "element-plus";

const tableData = ref([])

const id = ref(-1)

const dialogVisible = ref(false)

const dialogTitle = ref('')
const initMenuList = async () => {
  const res = await requestUtil.get('menu/tree/list')
  tableData.value = res.data.msg
}
initMenuList()

const handleDialogValue = (menuId) => {
  if (menuId) {
    id.value = menuId;
    dialogTitle.value = '菜单修改'
  } else {
    id.value = -1;
    dialogTitle.value = '菜单添加'
  }
  dialogVisible.value = true
}

const handleDelete = async (id) => {
  const res = await requestUtil.del('menu/action', id)
  if (res.data.code === 200) {
    ElMessage.success('删除成功')
    await initMenuList()
  } else {
    ElMessage.error(res.data.msg)
  }
}
</script>

<template>
  <div class="app-container">
    <el-card class="box-card">
      <template v-slot:header>
        <div class="space-between">
          <el-button type="warning" :icon="DocumentAdd" @click="handleDialogValue()">新增</el-button>
        </div>
      </template>
      <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px"
          row-key="id"
          height="600px"
          border
          stripe
          default-expand-all
          :tree-props="{children:'children',hasChildren:'hasChildren'}"
      >
        <el-table-column prop="name" label="菜单名称" width="180px"/>
        <el-table-column prop="icon" label="图标" width="70px" align="center">
          <template v-slot="scope">
            <el-icon :size="20">
              <component :is="scope.row.icon"/>
            </el-icon>
          </template>
        </el-table-column>
        <el-table-column prop="order_num" label="排序" width="70px" align="center"/>
        <el-table-column prop="perms" label="权限标识" width="200px"/>
        <el-table-column prop="path" label="组件路径" width="180px"/>
        <el-table-column property="menu_type" label="菜单类型" width="120" align="center">
          <template v-slot="scope">
            <el-tag size="small" v-if="scope.row.menuType === 'M'" type="danger" effect="dark">目录</el-tag>
            <el-tag size="small" v-else-if="scope.row.menuType === 'C'" type="success" effect="dark">菜单</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" align="center"/>
        <el-table-column prop="update_time" label="修改时间" align="center"/>
        <el-table-column prop="action" label="操作" width="300px" fixed="right" align="center">
          <template v-slot="scope">
            <el-button type="primary" :icon="Edit" @click="handleDialogValue(scope.row.id)"/>
            <el-popconfirm title="你确定要删除这条记录吗？" @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button type="danger" :icon="Delete"/>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <Dialog v-model="dialogVisible" :tableData="tableData" :dialogVisible="dialogVisible" :id="id"
            :dialogTitle="dialogTitle" @initMenuList="initMenuList"></Dialog>
  </div>
</template>

<style scoped lang="scss">
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}

.space-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 24px;
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

:deep(th.el-table__cell) {
  word-break: break-word;
  color: var(--text-color);
  height: 40px;
  font-size: 13px;
}

.el-tag--small {
  margin-left: 5px;
}
</style>
